package fun.cyhgraph.mapper;

import fun.cyhgraph.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 地址簿
 */
@Mapper
public interface AddressBookMapper {

    /**
     * 新增地址-添加地址簿
     * @param addressBook
     */
    @Insert("insert into address_book" +
            "        (user_id, consignee, phone, gender, province_code, province_name, city_code, city_name, district_code," +
            "         district_name, detail, label, is_default)" +
            "        values (#{userId}, #{consignee}, #{phone}, #{gender}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}," +
            "                #{districtCode}, #{districtName}, #{detail}, #{label}, #{isDefault})")
    void insert(AddressBook addressBook);



    /**
     * 根据当前用户信息查询地址列表 / 查询默认地址
     * (动态sql)
     * @param addressBook
     * @return
     */
    List<AddressBook> getUserAddress(AddressBook addressBook);



    /**
     * 根据id查询地址    (用户端-根据id查询地址) (用户端-用户提交订单)
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook getById(Integer id);


    /**
     * 修改地址
     * （动态sql）
     * @param addressBook
     */
    void udpate(AddressBook addressBook);


    /**
     * 设置默认地址
     * @param addressBook
     */
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
    void updateIsDefaultByUserId(AddressBook addressBook);


    /**
     * 根据id删除地址
     * @param id
     */
    @Delete("delete from address_book where id = #{id}")
    void delete(Integer id);

}
